<?php

/**
 * Description of Controller
 *
 * @author Lisette, Monica
 * @date: 2012-06-26 20:20
 * @modifications: 2012-06-26 23:00 Lisette
 */
class DocumentController extends BaseController {

    protected $_errors;
    protected $_messages;

    /**
     * Show all documents 
     */
    function index() {
        // $this->set("documentList", $this->_model->queryAll());
    }

    /**
     * Open (view) 1 document
     * @param type $id     
     */
    function view($id = null) {
        $this->set("document", $this->_model->load($id));
    }

    /**
     * previosly: upload.php 
     */
    public function create() {
        $this->set("document", $this->_model->create());
    }

    /**
     * Controller for uploading
     * @param unknown_type $user_id
     */
    public function add($parameters) {

        //echo "adding";
        //Authenticate the user
        performAction(null, "Users", "authenticate", array());

        //Get the logged user
        $user_id = intval($_SESSION['user_id']);

        $projects = performAction("Projects", "Projects", "queryByUserID", array($user_id));
        $this->set("projects", $projects);
        $this->set("allJournals", $this->_model->queryAllJournalsOrdered());
        $this->set("allSecondaryTitles", $this->_model->queryAllSecTitleOrdered());

        $numberOfCategories = performAction("Categories", "Categories", "queryNumberOfChategories", array());
        $this->set("numberOfCategories", $numberOfCategories);

        $categories = performAction("Categories", "Categories", "queryAllOrderBy", array("category"));
        $this->set("allCategories", $categories);

        $document = new Document();
        $document->setMetadata(new Metadata());
        $document->setFile(new File());
        $this->set("document", $document);

        if (!empty($_POST['title']) && isset($_POST['form_sent'])) {

            $saved = $this->save($parameters);

            $message = "";
            foreach ($this->_errors as $err) {
                if ($err != "")
                    $message .= $err . "[SPLIT]";
            }
            foreach ($this->_messages as $msg) {
                if ($msg != "")
                    $message .= $msg . "[SPLIT]";
            }
            $message .= "[END OF MESSAGE]";
            die($message);
        }
        elseif (isset($_POST['form_sent'])
                && (is_uploaded_file($_FILES['form_new_file']['tmp_name'])
                || !empty($_POST['form_new_file_link']))) {

            $rand = uniqid('lib_' . mt_rand(10, 99));
            $path = $parameters['temp_dir'];
            $saved = performAction(null, "File", "save", array($path, $rand, $pdf_url));

            if ($saved[0]) {
				
            	
            	$this->_messages = $saved[1];
                $this->set("tempfile", $rand . '.pdf');

                $pdf_meta = $this->extractPdfMeta($path . DIRECTORY_SEPARATOR . $rand . ".pdf");
                //echo $pdf_meta["doi"];
                //If no title is extracted from the Repositories
                //Extract tile from the PDF file
                $document_title = $document->getMetadata()->getTitle();
                $document->getFile()->setTmpName($rand);
                if (isset($pdf_meta["title"]) && (!isset($document_title) || $document_title == ''))
                    $document->getMetadata()->setTitle($pdf_meta["title"]);

                $title = $pdf_meta["title"];
                //If no abstract is extracted from the Repositories
                //Extract abstract from the PDF file
                $document_abstract = $document->getMetadata()->getAbstract();
                if (isset($pdf_meta["abstract"]) && (!isset($document_abstract) || $document_abstract == ''))
                    $document->getMetadata()->setAbstract($pdf_meta["abstract"]);

                $abstract = $pdf_meta["abstract"];
                
                

                if (isset($pdf_meta["doi"]))
                    $doi = $pdf_meta["doi"];

                //If no keywords are extracted from the Repositories
                //Extract keywords from the PDF file
                //$document->getMetadata()->setKeywords(array());
                $document_keywords = $document->getMetadata()->getKeywords();
               
                if (isset($pdf_meta["keywords"]) && empty($document_keywords)) {
                	
                	$k_arr = array();
                    $i = 0;

                    foreach ($pdf_meta["keywords"] as $k) {
                        $keyword = new Keyword();
                        $keyword->setKeyword($k);
                        $keyword->setSelected(true);
                        $k_arr[$i] = $keyword;
                        $i++;
                    }
                    $document->getMetadata()->setKeywords($k_arr);
                }
                $keywords = $k_arr;
            } else {
                $this->_errors = $saved[1];
            }

            if (!isset($doi))
                $this->set("document", $document);
        }

        // When the user clicks Fetch
        if (isset($_POST['form_sent']) && (!empty($doi)
                || !empty($_POST['doi']) || !empty($_POST['uid'][0])
                || !empty($arxiv_id)) || (!empty($title))) {

            if (!empty($_POST['doi']))
                $doi = trim($_POST['doi']);
            if (stripos($doi, 'doi:') === 0)
                $doi = trim(substr($doi, 4));

            foreach ($_POST['uid'] as $uid_element) {

                $uid_array2 = explode(":", $uid_element);
                $uid_array2[0] = trim(strtoupper($uid_array2[0]));
                if ($uid_array2[0] == 'PMID')
                    $pmid = trim($uid_array2[1]);
                if ($uid_array2[0] == 'ARXIV')
                    $arxiv_id = trim($uid_array2[1]);
                if ($uid_array2[0] == 'NASAADS')
                    $nasa_id = trim($uid_array2[1]);
                if ($uid_array2[0] == 'IEEE')
                    $ieee_id = trim($uid_array2[1]);
                if ($uid_array2[0] == 'ACM')
                    $acm_id = trim($uid_array2[1]);
            }



            if (!empty($doi))
                $doi = trim($doi);
            if (stripos($doi, 'doi:') === 0)
                $doi = trim(substr($doi, 4));

            // 2012-06-26, Dragan Milchevski: Implementation of Requirement ILF006 Parse a DOI-URL to a readable DOI format
            if (stripos($doi, 'http://dx.doi.org/') === 0)
                $doi = trim(substr($doi, 18));

            // Validating doi from the input to get only the 10.1111/xxxxxxxx part
            // Bug 
            $doi1 = $doi;
            preg_match('/10\.\d{4}\/\S+/ui', $doi1, $doi);
            $doi = current($doi);


            ##### Start with extracting metadata from the repositories #####
            //If only the DOI isset, nothing else
            if (isset($doi) && isset($_POST['uid'][0]) && $_POST['uid'][0] == "" && $doi != "") {

                //First look in the IEEE Digital Library
                RepositoryConnectionProperty::$_url = $parameters['repositories']['ieee'];
                RepositoryConnectionProperty::$_name = "IEEE";
                $document = performAction("IeeeRepository", "Repository", "searchByDoi", array($doi));


                //Continue searching in ACM Digital Library
                if ($document->getMetadata()->getTitle() == "") {
                    //RepositoryConnectionProperty::$_url = $request_url_acm;
                    RepositoryConnectionProperty::$_name = "ACM";
                    $document = performAction("AcmRepository", "Repository", "searchByDoi", array($doi));
                }

                //Continue searching in ACM Digital Library
                if ($document->getMetadata()->getTitle() == "") {
                    RepositoryConnectionProperty::$_url = $parameters['repositories']['springerlink'];
                    RepositoryConnectionProperty::$_name = "SPRINGERLINK";
                    $document = performAction("SpringerlinkRepository", "Repository", "searchByDoi", array($doi));
                }

                //Continue searching in PUBMED Digital Library
                if ($document->getMetadata()->getTitle() == "") {
                    RepositoryConnectionProperty::$_url = $parameters['repositories']['pubmed_doi'];
                    RepositoryConnectionProperty::$_name = "PUBMED";
                    $document = performAction("PubmedRepository", "Repository", "searchByDoi", array($doi));
                }


                //Continue searching in Nasa Digital Library
                if ($document->getMetadata()->getTitle() == "") {
                    RepositoryConnectionProperty::$_url = $parameters['repositories']['nasa'];
                    RepositoryConnectionProperty::$_name = "NASA";
                    $document = performAction("NasaRepository", "Repository", "searchByDoi", array($doi));
                }


                //At the end search in CrossRef
                if ($document->getMetadata()->getTitle() == "") {
                    RepositoryConnectionProperty::$_url = $parameters['repositories']['crossref'];
                    RepositoryConnectionProperty::$_name = "CROSSREF";
                    $document = performAction("CrossRefRepository", "Repository", "searchByDoi", array($doi));
                }
            }

            //If the Arxiv UID is set, search in the Arxiv Repository
            if (!empty($arxiv_id)) {

                RepositoryConnectionProperty::$_url = $parameters['repositories']['arxiv'];
                ;
                RepositoryConnectionProperty::$_name = "ARXIV";
                $document = performAction("ArxivRepository", "Repository", "searchByUid", array($arxiv_id));
            }

            //if the Pubmed UID is set, search in the Pubmed Repository
            if (!empty($pmid)) {

                RepositoryConnectionProperty::$_url = $parameters['repositories']['pubmed_pmid'];
                RepositoryConnectionProperty::$_name = "PUBMED";
                $document = performAction("PubmedRepository", "Repository", "searchByUid", array($pmid));
            }

            if (!empty($nasa_id)) {

                RepositoryConnectionProperty::$_url = $parameters['repositories']['nasa'];
                RepositoryConnectionProperty::$_name = "NASA";
                $document = performAction("NasaRepository", "Repository", "searchByUid", array(urlencode($nasa_id)));
                //var_dump($document);
            }
            if (!empty($ieee_id)) {

                RepositoryConnectionProperty::$_url = $parameters['repositories']['ieee'];
                RepositoryConnectionProperty::$_name = "IEEE";
                $document = performAction("IeeeRepository", "Repository", "searchByUid", array(urlencode($ieee_id)));
            }

            if (!empty($acm_id)) {
                RepositoryConnectionProperty::$_name = "ACM";
                $document = performAction("AcmRepository", "Repository", "searchByUid", array($acm_id));
            }
          
            if (!empty($title) && empty($doi)) {

                //if($document->getMetadata()->getTitle()!=""){

                RepositoryConnectionProperty::$_name = "ACM";
                $document = performAction("AcmRepository", "Repository", "searchByTitle", array($title));
                //}
               
                $this->set("document", $document);
            }


            $pdf_url = $document->getFile()->getUrl();
            //echo "pdf url=".$pdf_url."<br>";

            if (isset($pdf_url) && $pdf_url != '' && !isset($title)) {

                $rand = uniqid('lib_' . mt_rand(10, 99));
                $path = $parameters['temp_dir'];
                //echo "path=".$path." rand=".$rand." url=".$pdf_url;
                $saved = performAction(null, "File", "save", array($path, $rand, $pdf_url));
                if ($saved[0]) {
                    $this->_messages = $saved[1];
                    $this->set("tempfile", $rand . '.pdf');
                    $pdf_meta = $this->extractPdfMeta($path . DIRECTORY_SEPARATOR . $rand . ".pdf");

                    //If no title is extracted from the Repositories
                    //Extract tile from the PDF file
                    $document_title = $document->getMetadata()->getTitle();
                    if (isset($pdf_meta["title"]) && (!isset($document_title) || $document_title == ''))
                        $document->getMetadata()->setTitle($pdf_meta["title"]);

                    //If no abstract is extracted from the Repositories
                    //Extract abstract from the PDF file
                    $document_abstract = $document->getMetadata()->getAbstract();

                    if (isset($pdf_meta["abstract"]) && (!isset($document_abstract) || $document_abstract == ''))
                        $document->getMetadata()->setAbstract($pdf_meta["abstract"]);

                    //If no keywords are extracted from the Repositories
                    //Extract keywords from the PDF file
                    //$document->getMetadata()->setKeywords(array());
                    $document_keywords = $document->getMetadata()->getKeywords();

                    if (isset($pdf_meta["keywords"]) && empty($document_keywords)) {
                        
                    	
                    	$k_arr = array();
                        $i = 0;

                        foreach ($pdf_meta["keywords"] as $k) {
                        	
                            $keyword = new Keyword();
                            $keyword->setKeyword($k);
                            $keyword->setSelected(true);
                            $k_arr[$i] = $keyword;
                            $i++;
                        }
                        $document->getMetadata()->setKeywords($k_arr);
                    }
                } else {
                    $this->_errors = $saved[1];
                }
            }

            ##########	check for duplicate titles in table library	##########
			
            $abs = $document->getMetadata()->getAbstract();
            $key = $document->getMetadata()->getKeywords();
            $title1 = $document->getMetadata()->getTitle();
             
            if (empty($abs))
            	$document->getMetadata()->setAbstract($abstract);
            if (empty($key))
            	$document->getMetadata()->setKeywords($keywords);
            if (empty($title1))
            	$document->getMetadata()->setTitle($title);


            if (!empty($doi)) {
                $has_doi = $this->_model->queryByDoi($doi);
            } else {
                $has_title = $this->_model->queryByTitleRestrictLength($document->getMetadata()->getTitle());
            }

            //var_dump($has_doi);
            if ((isset($has_doi) && sizeof($has_doi) > 0) || (isset($has_title) && sizeof($has_title) > 0)) {

                if (isset($has_doi))
                    $d = $has_doi[0];
                else
                    $d = $has_title[0];
                $this->_errors[] = "Warning! This article is a possible duplicate of:<br>
    			<a href=\"" . htmlspecialchars("stable.php?id=" . urlencode($d->getId())) . "\" style=\"color:#fff;font-weight:bold\" target=\"_blank\">" . htmlspecialchars($d->getMetadata()->getTitle()) . "</a>";
            }


            //$paper_urls = array();
            //if (!empty($_POST['url']) && is_string($_POST['url'])) $paper_urls = explode('|', $_POST['url']);
            //$paper_uids = array();
            //if (!empty($_POST['uid']) && is_string($_POST['uid'])) $paper_uids = explode('|', $_POST['uid']);

            $this->set("document", $document);
        }

        if (!empty($_POST['title']) && !isset($_POST['form_sent'])) {

            ##########	check for duplicate titles in table library	##########
            if (!empty($_POST['doi'])) {
                $has_doi = $this->_model->queryByDoi($_POST['doi']);
            } else {
                $has_title = $this->_model->queryByTitleRestrictLength($_POST['title']);
            }

            //var_dump($has_doi);
            if ((isset($has_doi) && sizeof($has_doi) > 0) || (isset($has_title) && sizeof($has_title) > 0)) {
                if (isset($has_doi))
                    $d = $has_doi[0];
                else
                    $d = $has_title[0];
                $this->_errors[] = "Warning! This article is a possible duplicate of:<br>
	    		<a href=\"" . htmlspecialchars("stable.php?id=" . urlencode($d->getId())) . "\" style=\"color:#fff;font-weight:bold\" target=\"_blank\">" . htmlspecialchars($d->getMetadata()->getTitle()) . "</a>";
            }
        }

        if (!empty($this->_errors)) {
            foreach ($this->_errors as $err) {
                print '<div class="upload-errors" style="display:none">' . $err . '</div>';
            }
        }
    }

    /**
     * save new document 
     * @param type $parameters 
     */
    function save($parameters) {

        $user_id = intval($_SESSION['user_id']);
        //binding the data from the form
        $document = $this->bind();

        $message = "";
        $this->_messages = array();
        $this->_errors = array();
        //Start transaction
        $transaction = new Transaction();
        //saving the new record in the db
        $id = $this->_model->insert($document);

        if (isset($id)) {
            $this->_messages[] = "The item has been recorded.<br>" . $document->getMetadata()->getTitle() . "<br>";
        } else {
            $this->_errors[] = "Error! The item has not been recorded.<br>" . $document->getMetadata()->getTitle() . "<br>";
        }
        $transaction->commit();


        $category_ids = array();

        if (!empty($_POST['category2'])) {
            $_POST['category2'] = preg_replace('/\s{2,}/', '', $_POST['category2']);
            $_POST['category2'] = preg_replace('/^\s$/', '', $_POST['category2']);
            $_POST['category2'] = array_filter($_POST['category2']);

            $transaction = new Transaction();
            foreach ($_POST['category2'] as $cat) {
                $category = new Category();
                $category->setCategory($cat);
                $category_ids[] = performAction("Categories", "Categories", "insert", array($category));
            }
            $transaction->commit();
        }
        ####### record new relations into filescategories #########

        $categories = array();

        if (!empty($_POST['category']) || !empty($category_ids)) {
            $categories = array_merge((array) $_POST['category'], (array) $category_ids);
            $categories = array_filter(array_unique($categories));
        }

        $transaction = new Transaction();
        foreach ($categories as $cat) {
            if (!empty($id))
                $result = performAction("Categories", "Categories", "insertFilesCategories", array($id, $cat));
        }
        $transaction->commit();

        ##########	record publication data, table shelves	##########

        if (isset($_POST['shelf'])) {
            if (!empty($id))
                $shelf_inserted = $this->inserShelf($id, $user_id);

            if ($shelf_inserted < 1)
                $this->_errors[] = "Warning! The item has not been added to shelf.<br>" . $document->getMetadata()->getTitle() . "<br>";
            else
                $this->_messages[] = "The item has been added to shelf.<br>" . $document->getMetadata()->getTitle() . "<br>";
        }


        ##########	record to projectsfiles	##########

        if (isset($_POST['project']) && !empty($_POST['projectID'])) {
            if (!empty($id))
                $project_inserted = performAction("Projects", "Projects", "insertProjectsfiles", array($_POST['projectID'], $id));
            if ($project_inserted < 1)
                $this->_errors[] = "Warning! The item has not been added to the project." . $title . "<br>";
            else
                $this->_messages[] = "The item has been added to the project." . $title . "<br>";
        }

        ##########	record to clipboard	##########

        if (isset($_POST['clipboard'])) {
            session_start();
            $_SESSION['session_clipboard'][] = $id;
            $_SESSION['session_clipboard'] = array_unique($_SESSION['session_clipboard']);
            session_write_close();
        }


        $path = $parameters['library_path'];
        $filename = $this->getlastFilename($id);
        $rand = strstr($filename['file'], '.', true);
        $saved = performAction(null, "File", "save", array($path, $rand, null));


        if ($saved[0]) {
            $this->_messages[] = "The PDF file has been recorded.<br>" . $document->getMetadata()->getTitle() . "<br>";
            $this->extractPDFtoText($rand . '.pdf', $path, $id);
        }else
            $this->_errors[] = "Error! The PDF file has not been recorded.<br>" . $document->getMetadata()->getTitle() . "<br>";


        $sup_saved = performAction(null, "SupplementaryFiles", "saveFS", array($filename['file'], $parameters['library_path']));

        //print "<script type=\"text/javascript\">";
        //print "alert('error ".$sup_saved[0]."')";
        //print "</script>";
        if ($sup_saved[0]) {
            $this->_errors[] = $sup_saved[1]['errors'];
            $this->_messages[] = $sup_saved[1]['messages'];
        }

        return true;
    }

    /**
     * binding data from request
     * @param type $parameters
     * @return \Document 
     */
    private function bind() {
        $document = new Document();
        $document->setMetadata(new Metadata());
        $file = new File();

        $addedBy = new User();
        $addedBy->setId(intval($_SESSION['user_id']));
        $document->setAddedBy($addedBy);

        $name_list = $this->getNewFileName();
        $document->getFile()->setNewName($name_list['filename']);

        $order = array("\r\n", "\n", "\r");
        $keys = array('authors', 'affiliation', 'title', 'abstract', 'keywords');

        while (list($key, $field) = each($keys)) {
            if (!empty($_POST[$field])) {
                $_POST[$field] = str_replace($order, ' ', $_POST[$field]);
            }
        }

        empty($_POST['title']) ? $document->getMetadata()->setTitle("") : $document->getMetadata()->setTitle($_POST['title']);
        $document->getMetadata()->setTitleAscii(utf8_deaccent($_POST['title']));



        if (empty($_POST['authors'])) {
            $document->getMetadata()->setAuthors("");
            $document->getMetadata()->setAutorsAscii("");
        } else {
            $document->getMetadata()->setAuthors($_POST['authors']);
            $document->getMetadata()->setAutorsAscii(utf8_deaccent($_POST['authors']));
        }

        empty($_POST['affiliation']) ? $document->getMetadata()->setAffiliation("") : $document->getMetadata()->setAffiliation($_POST['affiliation']);


        empty($_POST['journal_abbr']) ? $document->getMetadata()->setJournal("") : $document->getMetadata()->setJournal($_POST['journal_abbr']);
        if (!empty($_POST['journal_abbr2']))
            $document->getMetadata()->setJournal($_POST['journal_abbr2']);

        empty($_POST['secondary_title']) ? $document->getMetadata()->setSecondaryTitle("") : $document->getMetadata()->setSecondaryTitle($_POST['secondary_title']);
        if (!empty($_POST['secondary_title2']))
            $document->getMetadata()->setSecondaryTitle($_POST['secondary_title2']);

        empty($_POST['year']) ? $document->getMetadata()->setYear("") : $document->getMetadata()->setYear($_POST['year']);
        $document->setAdditionDate(date('Y-m-d'));

        if (empty($_POST['abstract'])) {
            $document->getMetadata()->setAbstract("");
            $document->getMetadata()->setAbstractAscii("");
        } else {
            $document->getMetadata()->setAbstract($_POST['abstract']);
            $document->getMetadata()->setAbstractAscii(utf8_deaccent($_POST['abstract']));
        }

        empty($_POST['rating']) ? $document->getMetadata()->setRating("2") : $document->getMetadata()->setRating($_POST['rating']);

        empty($_POST['uid'][0]) ? $document->setUid("") : $document->setUid(join('|', array_filter($_POST['uid'])));

        empty($_POST['volume']) ? $document->getMetadata()->setVolume("") : $document->getMetadata()->setVolume($_POST['volume']);

        empty($_POST['issue']) ? $document->getMetadata()->setIssue("") : $document->getMetadata()->setIssue($_POST['issue']);

        empty($_POST['pages']) ? $document->getMetadata()->setPages("") : $document->getMetadata()->setPages($_POST['pages']);

        empty($_POST['editor']) ? $document->getMetadata()->setEditor("") : $document->getMetadata()->setEditor($_POST['editor']);

        empty($_POST['url'][0]) ? $document->getMetadata()->setUrl("") : $document->getMetadata()->setUrl(join('|', array_filter($_POST['url'])));

        empty($_POST['reference_type']) ? $document->getMetadata()->setReferenceType("") : $document->getMetadata()->setReferenceType($_POST['reference_type']);

        empty($_POST['publisher']) ? $document->getMetadata()->setPublisher("") : $document->getMetadata()->setPublisher($_POST['publisher']);

        empty($_POST['place_published']) ? $document->getMetadata()->setPlacePublished("") : $document->getMetadata()->setPlacePublished($_POST['place_published']);

        $keywords_array = array();
        $i = 0;
        //TODO: check the keywords get the selected ones
        if (!empty($_POST['keywords'])) {
            $keywords = $_POST['keywords'];
            foreach ($keywords as $k) {
                $keyword = new Keyword();
                $keyword->setKeyword($k);
                $keyword->setSelected(true);
                $keywords_array[$i] = $keyword;
                $i++;
            }
        }

        if (!empty($_POST['keywords2'])) {
            $keywords2 = $_POST['keywords2'];
            empty($_POST['keyword_separator']) ? $keyword_separator = '/' : $keyword_separator = $_POST['keyword_separator'];
            $keyword_array = explode($keyword_separator, $keywords2);

            function trim_value(&$value) {
                $value = trim($value);
            }

            array_walk($keyword_array, 'trim_value');

            foreach ($keyword_array as $k) {
                $keyword = new Keyword();
                $keyword->setKeyword($k);
                $keyword->setSelected(true);
                $keywords_array[$i] = $keyword;
                $i++;
            }
        }
        //$keywords = implode(' / ', $keyword_array);
        $document->getMetadata()->setKeywords($keywords_array);



        empty($_POST['doi']) ? $document->getMetadata()->setDoi("") : $document->getMetadata()->setDoi($_POST['doi']);

        empty($_POST['custom1']) ? $document->getMetadata()->setCustom1("") : $document->getMetadata()->setCustom1($_POST['custom1']);

        empty($_POST['custom2']) ? $document->getMetadata()->setCustom2("") : $document->getMetadata()->setCustom2($_POST['custom2']);

        empty($_POST['custom3']) ? $document->getMetadata()->setCustom3("") : $document->getMetadata()->setCustom3($_POST['custom3']);

        empty($_POST['custom4']) ? $document->getMetadata()->setCustom4("") : $document->getMetadata()->setCustom4($_POST['custom4']);

        // var_dump($document);
        return $document;
    }

    function update() {
        //in progress
        $this->_model->update($document);
    }

    function delete($id = null) {
        $this->_model->delete($id);
    }

    function clipboard() {
        
    }

    function shelf() {
        
    }

    protected function inserShelf($document_id, $user_id) {
        return $this->_model->insertShelf($document_id, $user_id);
    }

    function getlastFilename($id) {
        return $this->_model->getlastFilename($id);
    }

    private function getNewFileName() {
        return $this->_model->getNewFileName();
    }

    private function extractPdfMeta($file) {
        //echo "extracting..";
        $return_array = array();

        if (is_readable($file)) {
            if (file_exists($file)) {
                //Try to extract metadata from the PDF Metadata
                exec(select_pdfinfo() . '"' . $file . '"', $output);
                foreach ($output as $line) {
                    if (stripos($line, 'title: ') === 0) {
                        $title_pdf = 'Unknown file ' . substr($line, 7);
                    }
                    if (stripos($line, 'pages: ') === 0)
                        $pages = substr($line, 7);
                }
            }


            ##########	try to find DOI on the first page	##########

            system(select_pdftotext() . '-l 1 "' . $file . '" "' . $file . '.txt"', $ret);

            if (is_file($file . ".txt")) {

                $string = file_get_contents($file . ".txt");
                unlink($file . ".txt");
            }

            if (!empty($string)) {

                ##### @author Evica Ilieva
                //Put every line from the file into a single entry in a array
                $string_array = explode("\n", $string);

               // print "<pre>" . print_r($string_array, true) . "</pre>";
                //Trim the array. Delete the empty rows and trim the values in the array
                $string_array = array_map('trim', $string_array);

                $i = 0;
                $tmp_arr = array();
                foreach ($string_array as $line) {
                    if ($line != '') {
                        $tmp_arr[$i] = $line;
                        $i++;
                    }
                }
                //$string_array = $tmp_arr;
                #### Relevant for Requirements ILF002, ILF003, ILF015
                //Get the title of the paper from the PDF
                $title_pdf = $string_array[0];
                //echo $title_pdf."<br>";
                #### Relevant for Requirement ILF017
                //Search for keywords inside the PDF
                $keywords_search = array();
                $tmp1 = preg_grep("/terms/i", $string_array);
                $tmp2 =preg_grep("/key\s*words/i", $string_array);
                $keywords_search = $tmp1 + $tmp2;
                 
               // print "<pre>" . print_r($keywords_search, true) . "</pre>";
                if(!empty($keywords_search)){
                
                	$keywords_array = array();
                	$i=0;
                	$keywords_keys = array_keys($keywords_search);
                	foreach(array_keys($keywords_search) as $key){
                		$k = $key;
                
                		//if it contains keywords, then it is the next row
                		if(stripos($string_array[$k], "keywords" ) !== false){
                			$k++;
                		}elseif(stripos($string_array[$k], "terms" ) !== false){
                			$k++;
                		}
                
                		if(empty($string_array[$k]) )
                			$k--;
                		 
                		$keyword_string1 = stristr($string_array[$k], "keywords");
                		if(empty($keyword_string1)){
                			$keyword_string1 = stristr($string_array[$k], "key words");
                		}
                		if(empty($keyword_string1)){
                			$keyword_string1 = stristr($string_array[$k], "terms");
                		}
                		if(!empty($keyword_string1)){
                			$keyword_string = $keyword_string1;
                		}else{
                			$keyword_string = $string_array[$k];
                		}
                
                
                		$keyword_string = trim($keyword_string, "keywords:");
                		$keyword_string = trim($keyword_string, "keyword:");
                		$keyword_string = trim($keyword_string, "Keywords:");
                		$keyword_string = trim($keyword_string, "Keywords");
                		$keyword_string = trim($keyword_string, "key words:");
                		$keyword_string = trim($keyword_string, "Key words:");
                		$keyword_string = trim($keyword_string, "Key words");
                		$keyword_string = trim($keyword_string, "key words");
                		$keyword_string = trim($keyword_string, "Terms");
                		$keyword_string = trim($keyword_string, "Terms:");
                		$keyword_string = trim($keyword_string, "terms:");
                		$keyword_string = trim($keyword_string, "terms");
                
                		$keyword_string = str_replace(chr(226), ';', $keyword_string);
                		$keyword_string = str_replace(chr(128), '', $keyword_string);
                		$keyword_string = str_replace(chr(148), '', $keyword_string);
                		//echo "keywords ".$keyword_string;
                		//for($p=0;$p<strlen($keyword_string); $p++)
                		//echo $keyword_string[$p]."--".ord($keyword_string[$p])."<br>";
                
                		$tmp_arr = preg_split( "/[-,;|]/", $keyword_string);
                
                		foreach($tmp_arr as $r){
                			
                			$keywords_array[$i]=trim($r);
                			$i++;
                		}
                	}
                	 
                }

                #### To be used when the abstract from the Document object is not populated. Good case is when extracting metadata from ACM.
                //Search fro abstract inside the PDF
                $abstract_search = preg_grep("/abstract*/i", $string_array);
                $search_keys = array_keys($abstract_search);
              //  print "<pre>" . print_r($abstract_search, true) . "</pre>";
                $abstract = "";
                if (!empty($search_keys)) {
                    if (strlen($string_array[$search_keys[0]]) > 100) {
                        $abstract = $string_array[$search_keys[0]];
                        $k = $search_keys[0];
                    } else {
                        $abstract = $string_array[$search_keys[0] + 1];
                        $k = $search_keys[0] + 1;
                    }
                    if (empty($string_array[$k]))
                        $k++;
                    $abstract = "";
                    
                    while (!empty($string_array[$k])) {
                        
                    	$abstract.=$string_array[$k];
                        $k++;
                    }
                }
				
                //echo "<br>abstract=".$abstract;

                $string = preg_replace('/[^[\x09\x0A\x0D\x20-\x7E]|[\xC2-\xDF][\x80-\xBF]|\xE0[\xA0-\xBF][\x80-\xBF]|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}|\xED[\x80-\x9F][\x80-\xBF]|\xF0[\x90-\xBF][\x80-\xBF]{2}|[\xF1-\xF3][\x80-\xBF]{3}|\xF4[\x80-\x8F][\x80-\xBF]{2}]/', ' ', $string);

                $order = array("\r\n", "\n", "\r");
                $replace = ' ';
                $string = str_replace($order, $replace, $string);

                $order = array("\xe2\x80\x93", "\xe2\x80\x94");
                $replace = '-';
                $string = str_replace($order, $replace, $string);

                preg_match('/10\.\d{4}\/\S+/ui', $string, $doi);
                $doi = current($doi);

                if (substr($doi, -1) == ')' || substr($doi, -1) == ']') {
                    preg_match_all('/(.)(doi:\s?)?(10\.\d{4}\/\S+)/ui', $string, $doi2, PREG_PATTERN_ORDER);
                    if (substr($doi, -1) == ')' && $doi2[1][0] == '(')
                        $doi = substr($doi, 0, -1);
                    if (substr($doi, -1) == ']' && $doi2[1][0] == '[')
                        $doi = substr($doi, 0, -1);
                }

                preg_match('/(?<=arXiv:)\S+/ui', $string, $arxiv_id);
                $arxiv_id = current($arxiv_id);
            }

            if (empty($doi) && empty($arxiv_id)) {

                ##########	try to find DOI in the whole PDF	##########
                if (isset($pages))
                    $prm = " -l " . $pages / 2;
                else
                    $prm = "";
                system(select_pdftotext() . $prm . ' "' . $file . '.pdf" "' . $file . '.txt"', $ret);

                if (is_file($file . ".txt")) {

                    $string = file_get_contents($file . ".txt");
                    unlink($file . ".txt");
                }

                if (!empty($string)) {

                    ##### @author Evica Ilieva
                    //Put every line from the file into a single entry in a array
                    $string_array = explode("\n", $string);

                    //Trim the array. Delete the empty rows and trim the values in the array
                    $string_array = array_map('trim', $string_array);
                    $i = 0;
                    $tmp_arr = array();
                    foreach ($string_array as $line) {
                        if ($line != '') {
                            $tmp_arr[$i] = $line;
                            $i++;
                        }
                    }
                    $string_array = $tmp_arr;

                    #### Relevant for Requirements ILF002, ILF003, ILF015
                    //Get the title of the paper from the PDF
                    if (!isset($title_pdf))
                        $title_pdf = $string_array[0];


                    //echo "Keywords:<br>";
                    //print "<pre>" . print_r($keywords_array, true) . "</pre>";
                    if (!isset($abstract)) {
                        #### To be used when the abstract from the Document object is not populated. Good case is when extracting metadata from ACM.
                        //Search fro abstract inside the PDF
                        $abstract_search = preg_grep("/abstract*/i", $string_array);
                        $search_keys = array_keys($abstract_search);
                        $abstract = $string_array[$search_keys[0] + 1];
                        //echo "Abstract: <br>".$abstract;
                    }
                    $string = preg_replace('/[^[\x09\x0A\x0D\x20-\x7E]|[\xC2-\xDF][\x80-\xBF]|\xE0[\xA0-\xBF][\x80-\xBF]|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}|\xED[\x80-\x9F][\x80-\xBF]|\xF0[\x90-\xBF][\x80-\xBF]{2}|[\xF1-\xF3][\x80-\xBF]{3}|\xF4[\x80-\x8F][\x80-\xBF]{2}]/', ' ', $string);

                    $order = array("\r\n", "\n", "\r");
                    $replace = ' ';
                    $string = str_replace($order, $replace, $string);

                    $order = array("\xe2\x80\x93", "\xe2\x80\x94");
                    $replace = '-';
                    $string = str_replace($order, $replace, $string);

                    preg_match('/10\.\d{4}\/\S+/ui', $string, $doi);
                    $doi = current($doi);

                    if (substr($doi, -1) == ')' || substr($doi, -1) == ']') {
                        preg_match_all('/(.)(doi:\s?)?(10\.\d{4}\/\S+)/ui', $string, $doi2, PREG_PATTERN_ORDER);
                        if (substr($doi, -1) == ')' && $doi2[1][0] == '(')
                            $doi = substr($doi, 0, -1);
                        if (substr($doi, -1) == ']' && $doi2[1][0] == '[')
                            $doi = substr($doi, 0, -1);
                    }

                    preg_match('/(?<=arXiv:)\S+/ui', $string, $arxiv_id);
                    $arxiv_id = current($arxiv_id);
                }
            }
        }

        $return_array["doi"] = $doi;
        $return_array["arxiv_id"] = $arxiv_id;
        $return_array["title"] = $title_pdf;
        $return_array["abstract"] = $abstract;
        $return_array["keywords"] = $keywords_array;

        //print "<pre>" . print_r($return_array, true) . "</pre>";

        return $return_array;
    }

    private function extractPDFtoText($filename, $path, $document_id) {

        $temp_dir = sys_get_temp_dir();
        if (substr($temp_dir, -1) == DIRECTORY_SEPARATOR)
            $temp_dir = substr($temp_dir, 0, -1);

        system(select_pdftotext() . '"' . $path . DIRECTORY_SEPARATOR . $filename . '" "' . $temp_dir . DIRECTORY_SEPARATOR . $filename . '.txt"', $ret);


        if (is_file($temp_dir . DIRECTORY_SEPARATOR . $filename . ".txt")) {

            $stopwords = "a's, able, about, above, according, accordingly, across, actually, after, afterwards, again, against, ain't, all, allow, allows, almost, alone, along, already, also, although, always, am, among, amongst, an, and, another, any, anybody, anyhow, anyone, anything, anyway, anyways, anywhere, apart, appear, appreciate, appropriate, are, aren't, around, as, aside, ask, asking, associated, at, available, away, awfully, be, became, because, become, becomes, becoming, been, before, beforehand, behind, being, believe, below, beside, besides, best, better, between, beyond, both, brief, but, by, c'mon, c's, came, can, can't, cannot, cant, cause, causes, certain, certainly, changes, clearly, co, com, come, comes, concerning, consequently, consider, considering, contain, containing, contains, corresponding, could, couldn't, course, currently, definitely, described, despite, did, didn't, different, do, does, doesn't, doing, don't, done, down, downwards, during, each, edu, eg, eight, either, else, elsewhere, enough, entirely, especially, et, etc, even, ever, every, everybody, everyone, everything, everywhere, ex, exactly, example, except, far, few, fifth, first, five, followed, following, follows, for, former, formerly, forth, four, from, further, furthermore, get, gets, getting, given, gives, go, goes, going, gone, got, gotten, greetings, had, hadn't, happens, hardly, has, hasn't, have, haven't, having, he, he's, hello, help, hence, her, here, here's, hereafter, hereby, herein, hereupon, hers, herself, hi, him, himself, his, hither, hopefully, how, howbeit, however, i'd, i'll, i'm, i've, ie, if, ignored, immediate, in, inasmuch, inc, indeed, indicate, indicated, indicates, inner, insofar, instead, into, inward, is, isn't, it, it'd, it'll, it's, its, itself, just, keep, keeps, kept, know, knows, known, last, lately, later, latter, latterly, least, less, lest, let, let's, like, liked, likely, little, look, looking, looks, ltd, mainly, many, may, maybe, me, mean, meanwhile, merely, might, more, moreover, most, mostly, much, must, my, myself, name, namely, nd, near, nearly, necessary, need, needs, neither, never, nevertheless, new, next, nine, no, nobody, non, none, noone, nor, normally, not, nothing, novel, now, nowhere, obviously, of, off, often, oh, ok, okay, old, on, once, one, ones, only, onto, or, other, others, otherwise, ought, our, ours, ourselves, out, outside, over, overall, own, particular, particularly, per, perhaps, placed, please, plus, possible, presumably, probably, provides, que, quite, qv, rather, rd, re, really, reasonably, regarding, regardless, regards, relatively, respectively, right, said, same, saw, say, saying, says, second, secondly, see, seeing, seem, seemed, seeming, seems, seen, self, selves, sensible, sent, serious, seriously, seven, several, shall, she, should, shouldn't, since, six, so, some, somebody, somehow, someone, something, sometime, sometimes, somewhat, somewhere, soon, sorry, specified, specify, specifying, still, sub, such, sup, sure, t's, take, taken, tell, tends, th, than, thank, thanks, thanx, that, that's, thats, the, their, theirs, them, themselves, then, thence, there, there's, thereafter, thereby, therefore, therein, theres, thereupon, these, they, they'd, they'll, they're, they've, think, third, this, thorough, thoroughly, those, though, three, through, throughout, thru, thus, to, together, too, took, toward, towards, tried, tries, truly, try, trying, twice, two, un, under, 				unfortunately, unless, unlikely, until, unto, up, upon, us, use, used, useful, uses, using, usually, value, various, very, via, viz, vs, want, wants, was, wasn't, way, we, we'd, we'll, we're, we've, welcome, well, went, were, weren't, what, what's, whatever, when, whence, whenever, where, where's, whereafter, whereas, whereby, wherein, whereupon, wherever, whether, which, while, whither, who, who's, whoever, whole, whom, whose, why, will, willing, wish, with, within, without, won't, wonder, would, would, wouldn't, yes, yet, you, you'd, you'll, you're, you've, your, yours, yourself, yourselves, zero";

            $stopwords = explode(', ', $stopwords);

            $string = file_get_contents($temp_dir . DIRECTORY_SEPARATOR . $filename . ".txt");
            unlink($temp_dir . DIRECTORY_SEPARATOR . $filename . ".txt");

            if (!empty($string)) {

                $string = preg_replace('/[^[\x09\x0A\x0D\x20-\x7E]|[\xC2-\xDF][\x80-\xBF]|\xE0[\xA0-\xBF][\x80-\xBF]|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}|\xED[\x80-\x9F][\x80-\xBF]|\xF0[\x90-\xBF][\x80-\xBF]{2}|[\xF1-\xF3][\x80-\xBF]{3}|\xF4[\x80-\x8F][\x80-\xBF]{2}]/', ' ', $string);

                $patterns = join("\b/ui /\b", $stopwords);
                $patterns = "/\b$patterns\b/ui";
                $patterns = explode(" ", $patterns);

                $order = array("\r\n", "\n", "\r");
                $string = str_replace($order, ' ', $string);
                $string = preg_replace($patterns, '', $string);
                $string = preg_replace('/\s{2,}/ui', ' ', $string);

                $fulltext_array = array();
                $fulltext_unique = array();

                $fulltext_array = explode(" ", $string);
                $fulltext_unique = array_unique($fulltext_array);
                $string = implode(" ", $fulltext_unique);


                ConnectionProperty::$database_name = 'fulltext';
                $transaction = new Transaction();
                $this->_model->deleteFullText($document_id);
                $this->_model->insertFullText($document_id, $string);

                $transaction->commit();
                ConnectionProperty::$database_name = 'library';
            } else {

                $this->_errors[] = "Warning! The PDF file cannot be indexed for full text search.";
            }
        }
    }    
    
    
    public function batch($parameters) {

        $log = "";
        $html_log = "";
        $html_log_2 = "";
        $user_id = intval($_SESSION['user_id']);

        $projects = performAction("Projects", "Projects", "queryByUserID", array($user_id));
        $this->set("projects", $projects);

        $numberOfCategories = performAction("Categories", "Categories", "queryNumberOfChategories", array());
        $this->set("numberOfCategories", $numberOfCategories);

        $categories = performAction("Categories", "Categories", "queryAllOrderBy", array("category"));
        $this->set("allCategories", $categories);

        $this->set("batchimport_failed", "2");

        $this->set("path", $parameters['library_path']);
        
        $timestamp1 = date("Y-m-d H:i:s");
        $log .="Batch Import started at ".$timestamp1."\n\n";
        $no_success = 0;
        $no_failed = 0;
        $starttime = time();

        if (isset($_FILES['files'])) {
            for ($i = 0; $i < count($_FILES['files']['name']); $i++) {
                if (is_uploaded_file($_FILES['files']['tmp_name'][$i])) {

                    $file = $_FILES['files']['tmp_name'][$i];
                    $orig_filename = $_FILES['files']['name'][$i];
                    $timestamp = date("Y-m-d H:i:s");

                    if (is_readable($file)) {


                        $pdf_meta = $this->extractPdfMeta($file);
                        $doi = $pdf_meta["doi"];

                        $document = new Document();
                        $document->setMetadata(new Metadata());
                        $document->setFile(new File());

                        if (isset($doi)) {
                            foreach ($_POST['database'] as $database) {
                                switch ($database) {
                                    case "ieee": $ieee = true;
                                        break;
                                    case "acm" : $acm = true;
                                        break;
                                    case "springerlink" : $springerlink = true;
                                        break;
                                    case "pubmed" : $pubmed = true;
                                        break;
                                    case "nasa" : $nasa = true;
                                        break;
                                    case "crossref" : $crossref = true;
                                        break;
                                }
                            }
                            $found = false;
                            if ($document->getMetadata()->getTitle() == "" && isset($ieee) && $ieee == true) {
                                //First look in the IEEE Digital Library
                                RepositoryConnectionProperty::$_url = $parameters['repositories']['ieee'];
                                RepositoryConnectionProperty::$_name = "IEEE";
                                $document = performAction("IeeeRepository", "Repository", "searchByDoi", array($doi));
                                $title_tmp = $document->getMetadata()->getTitle();
                                if (!empty($title_tmp))
                                    $found = true;
                            }

                            //Continue searching in ACM Digital Library
                            if ($document->getMetadata()->getTitle() == "" && isset($acm) && $acm == true) {
                                RepositoryConnectionProperty::$_name = "ACM";
                                $document = performAction("AcmRepository", "Repository", "searchByDoi", array($doi));
                                $title_tmp = $document->getMetadata()->getTitle();
                                if (!empty($title_tmp))
                                    $found = true;
                            }

                            //Continue searching in Springerlink Digital Library
                            if ($document->getMetadata()->getTitle() == "" && isset($springerlink) && $springerlink == true) {
                                RepositoryConnectionProperty::$_url = $parameters['repositories']['springerlink'];
                                RepositoryConnectionProperty::$_name = "SPRINGERLINK";
                                $document = performAction("SpringerlinkRepository", "Repository", "searchByDoi", array($doi));

                                $title_tmp = $document->getMetadata()->getTitle();
                                if (!empty($title_tmp))
                                    $found = true;
                            }

                            //Continue searching in PUBMED Digital Library
                            if ($document->getMetadata()->getTitle() == "" && isset($pubmed) && $pubmed == true) {
                                RepositoryConnectionProperty::$_url = $parameters['repositories']['pubmed_doi'];
                                RepositoryConnectionProperty::$_name = "PUBMED";
                                $document = performAction("PubmedRepository", "Repository", "searchByDoi", array($doi));

                                $title_tmp = $document->getMetadata()->getTitle();
                                if (!empty($title_tmp))
                                    $found = true;
                            }

                            //Continue searching in Nasa Digital Library
                            if ($document->getMetadata()->getTitle() == "" && isset($nasa) && $nasa == true) {
                                RepositoryConnectionProperty::$_url = $parameters['repositories']['nasa'];
                                RepositoryConnectionProperty::$_name = "NASA";
                                $document = performAction("NasaRepository", "Repository", "searchByDoi", array($doi));

                                $title_tmp = $document->getMetadata()->getTitle();
                                if (!empty($title_tmp))
                                    $found = true;
                            }

                            //At the end search in CrossRef
                            if ($document->getMetadata()->getTitle() == "" && isset($crossref) && $crossref == true) {
                                RepositoryConnectionProperty::$_url = $parameters['repositories']['crossref'];
                                RepositoryConnectionProperty::$_name = "CROSSREF";
                                $document = performAction("CrossRefRepository", "Repository", "searchByDoi", array($doi));

                                $title_tmp = $document->getMetadata()->getTitle();
                                if (!empty($title_tmp))
                                    $found = true;
                            }

                            if (isset($pdf_meta["abstract"]))
                                $abstract = $pdf_meta["abstract"];

                            if (isset($pdf_meta["title"]))
                                $title = $pdf_meta["title"];

                            //If no keywords are extracted from the Repositories
                            //Extract keywords from the PDF file
                            //$document->getMetadata()->setKeywords(array());
                            $document_keywords = $document->getMetadata()->getKeywords();

                            if (isset($pdf_meta["keywords"]) && empty($document_keywords)) {
                                $k_arr = array();
                                $j = 0;

                                foreach ($pdf_meta["keywords"] as $k) {
                                    $keyword = new Keyword();
                                    $keyword->setKeyword($k);
                                    $keyword->setSelected(true);
                                    $k_arr[$j] = $keyword;
                                    $j++;
                                }
                                $document->getMetadata()->setKeywords($k_arr);
                            }

                            $abs = $document->getMetadata()->getAbstract();
                            $key = $document->getMetadata()->getKeywords();
                            $title1 = $document->getMetadata()->getTitle();
                            if (empty($abs))
                                $document->getMetadata()->setAbstract($abstract);
                            if (empty($key))
                                $document->getMetadata()->setKeywords($keywords);
                            if (empty($title1))
                                $document->getMetadata()->setTitle($title);



                            $addedBy = new User();
                            $addedBy->setId($user_id);
                            $document->setAddedBy($addedBy);
                            $document->setAdditionDate(date('Y-m-d'));

                            $name_list = $this->getNewFileName();
                            $document->getFile()->setNewName($name_list['filename']);


                            if (!$found && isset($_POST['failed']) && $_POST['failed'] == '3') {

                                $html_log_2 .= "<li>" . basename($orig_filename) . ": Not recorded. Metadata not found! File not saved.</li>";

                                $html_log .= "<span style='color:#FF0000'>" . basename($orig_filename) . ": Not recorded. Metadata not found. File not saved.</span><br>";
                                $log .= "[" . $timestamp . "] " . basename($orig_filename) . ": Not recorded. Metadata not found. File not saved! DOI not found.";
                                $no_failed ++;
                                
                            } elseif (!$found && (isset($_POST['failed']) && $_POST['failed'] == '2')) {

                                $path = $parameters['library_path'];

                                //print "<script type=\"text/javascript\">";
                                //print "alert('error ".$sup_saved[0]."')";
                                //print "</script>";

                                $saved = copy($file, $path . "batch" . DIRECTORY_SEPARATOR . basename($orig_filename));

                                if ($saved) {
                                    //$put = basename($orig_filename).": copying disallowed.<br>";
                                    $html_log_2 .= "<li>" . basename($orig_filename) . ": Not Recorded. File saved to the batch directory. DOI not found.</li>";
                                    
                                    $html_log = "<span style='color:#0F0'>" . basename($orig_filename) . ": Not Recorded. File saved to the batch directory!</span><br>";
                                    $log .= "[" . $timestamp . "] " . basename($orig_filename) . ": Not Recorded. File saved to the batch directory. DOI not found.\n";
                                    $this->extractPDFtoText($rand . '.pdf', $path, $id);
                                    $no_success ++;
                                } else {
                                    $html_log_2 .= "<li>" . basename($orig_filename) . ": Not recorded. File could not be saved!</li>";

                                    $html_log .= "<span style='color:#FF0000'>" . basename($orig_filename) . ": Not recorded. File could not be saved!</span><br>";
                                    $log .= "[" . $timestamp . "] " . basename($orig_filename) . ": Not recorded. File could not be saved!\n";
                                    $no_failed ++;
                                }
                                

                            }elseif (($found || (isset($_POST['failed']) && $_POST['failed'] == '1')) && $document->getMetadata()->getTitle() != "") {
                                //Start transaction
                                $transaction = new Transaction();
                                //saving the new record in the db
                                $id = $this->_model->insert($document);


                                $transaction->commit();


                                ##########	record to projectsfiles	##########

                                if (isset($_POST['project']) && !empty($_POST['projectID'])) {
                                    if (!empty($id))
                                        $project_inserted = performAction("Projects", "Projects", "insertProjectsfiles", array($_POST['projectID'], $id));
                                    if ($project_inserted < 1)
                                        $put = htmlspecialchars("Warning! The item has not been added to the project." . $title);
                                }


                                $category_ids = array();

                                if (!empty($_POST['category2'])) {
                                    $_POST['category2'] = preg_replace('/\s{2,}/', '', $_POST['category2']);
                                    $_POST['category2'] = preg_replace('/^\s$/', '', $_POST['category2']);
                                    $_POST['category2'] = array_filter($_POST['category2']);

                                    $transaction = new Transaction();
                                    foreach ($_POST['category2'] as $cat) {
                                        $category = new Category();
                                        $category->setCategory($cat);
                                        $category_ids[] = performAction("Categories", "Categories", "insert", array($category));
                                    }
                                    $transaction->commit();
                                }
                                ####### record new relations into filescategories #########

                                $categories = array();

                                if (!empty($_POST['category']) || !empty($category_ids)) {
                                    $categories = array_merge((array) $_POST['category'], (array) $category_ids);
                                    $categories = array_filter(array_unique($categories));
                                }

                                $transaction = new Transaction();
                                foreach ($categories as $cat) {
                                    if (!empty($id))
                                        $result = performAction("Categories", "Categories", "insertFilesCategories", array($id, $cat));
                                }
                                $transaction->commit();

                                ##########	record publication data, table shelves	##########

                                if (isset($_POST['shelf'])) {
                                    if (!empty($id))
                                        $shelf_inserted = $this->inserShelf($id, $user_id);

                                    if ($shelf_inserted < 1)
                                        $put = htmlspecialchars("Warning! The item has not been added to shelf.<br>" . $document->getMetadata()->getTitle());
                                }


                                $path = $parameters['library_path'];
                                $filename = $this->getlastFilename($id);
                                $rand = strstr($filename['file'], '.', true);

                                $saved = copy($file, $path . $filename['file']);

                                if ($saved && !empty($id)) {
                                    //$put = basename($orig_filename).": Recorded.<br>";
                                    $html_log_2 .= "<li>" . basename($orig_filename) . ": Recorded!</li>";

                                    $html_log .= "<span style='color:#0F0'>" . basename($orig_filename) . ": Recorded!</span><br>";
                                    $log .= "[" . $timestamp . "] " . basename($orig_filename) . ": Recorded!\n";
                                    $this->extractPDFtoText($rand . '.pdf', $path, $id);
                                    $no_success ++;
                                } else {
                                    //$put = basename($orig_filename).": Not Recorded.</span><br>";
                                    $html_log_2 .= "<li>" . basename($orig_filename) . ": Not Recorded!</li>";

                                    $html_log .= "<span style='color:#FF0000'>" . basename($orig_filename) . ": Not Recorded!</span><br>";
                                    $log .= "[" . $timestamp . "] " . basename($orig_filename) . ": Not Recorded!\n";
                                    $no_failed ++;
                                }
                                
                            }
                        }
                        
                       
                    }
                }
            }
        }

        if (!empty($html_log_2)) {
            //Write a log file
        	$timestamp2 = date("Y-m-d H:i:s");
        	$endtime = time();
        	$log .= "\n\nBatch Import finished at ".$timestamp2;
        	$log .= "\n$i PDF processed. $no_success files successfuly imported, $no_failed files failed.";
        	$log .= "\nThe import took ".round(abs($endtime - $starttime) / 60,2). " minutes";
            $t = mktime();
            $log .="[END OF LOG]";
            $log = substr($log, 0, strpos($log, "[END OF LOG]"));
            $logfile = "library/batch/log" . $t . ".txt";
            $fh = fopen($logfile, 'w') or die("can't open file");
            fwrite($fh, $log);
            fclose($fh);

            $html_log.="<br>See the detail log <a href='library/batch/log" . $t . ".txt' target='_blank'>here</a>";
            $html_log.= "[END OF MESSAGE]";
            
            if($i>20)
            	$html_log_2 = "Batch Import finished. <br>See the detail log <a href='library/batch/log".$t.".txt' target='_blank'>here</a>";
            else{
	            $html_log_2  = "<ol>".$html_log_2."</ol>";
	            $html_log_2 .= "<br>See the detail log <a href='library/batch/log" . $t . ".txt' target='_blank'>here</a>";
            }
            $html_log_2 .= "[END OF MESSAGE]";
            
            die($html_log_2);
        }
    }

}

?>
